package com.cooper.scraper.show;

import java.util.List;

import com.cooper.android.comm.Resource;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;

public abstract class FanartsShow<T extends Resource> {

	protected Context context;
	protected FanartViewStrategy<T> fanartStrategy;
	protected ResourceShowListener<T> showListener;

	private int watingImagRes;

	public int getWatingImagRes() {
		return watingImagRes;
	}

	public void setWatingImagRes(int watingImagRes) {
		this.watingImagRes = watingImagRes;
	}

	public FanartsShow(Context context, FanartViewStrategy<T> fanartStrategy,
			int watingImageRes) {
		super();
		this.context = context.getApplicationContext();
		this.fanartStrategy = fanartStrategy;
		this.watingImagRes = watingImageRes;
	}

	protected void init() {
		fanartStrategy.init(getFanartImgStrategy(),
				getFanartShowCount());
	}

	public abstract int getFanartShowCount();

	public abstract void setCommands(List<ResourceCommandAction<T>> commands);

	protected abstract FanartImgStrategy getFanartImgStrategy();

	protected abstract AdapterView<?> getAdapterView();

	public void configurationChanged(Configuration configuration,
			Display display) {
		waitingImg = null;
		fanartStrategy.configurationChanged(configuration, display,
				getFanartShowCount());
	}

	public void setMediasShowListener(ResourceShowListener<T> showListener) {
		this.showListener = showListener;
	}

	public void setAdapter(FanartAdapter<T> fanartAdapter) {
		fanartAdapter.setFanartsShow(this);
		fanartAdapter.refresh();
	}

	public void registerContextMenu(Activity activity) {
		activity.registerForContextMenu(getAdapterView());
	}

	public void registerContextMenu(Fragment fragment) {
		fragment.registerForContextMenu(getAdapterView());
	}

	@SuppressWarnings("unchecked")
	public T getSelectedResource() {
		return (T) getAdapterView().getSelectedItem();
	}

	public void destroy() {
		getAdapterView().setAdapter(null);
		fanartStrategy.destroy();
	}

	public abstract void removeFrom(ViewGroup from);

	public abstract void showTo(ViewGroup to);

	public abstract void setSelection(int position, boolean animate);

	public abstract void hideOrShowFanartsView();

	public abstract void resetSelected();

	public void resetFanart() {
		fanartStrategy.reset();
	}

	@SuppressWarnings("unchecked")
	public View getResourceView(T media, View convertView, ViewGroup parent) {
		FanartViewHolder<T> holder;
		if (convertView != null) {
			holder = (FanartViewHolder<T>) convertView.getTag();
			fanartStrategy.onConverView(holder);
		} else {
			holder = new SingleImgViewHolder<T>(context);
		}
		int viewType = FanartViewStrategy.FANART_VIEW_TYPE;

		Bitmap result = fanartStrategy.getMediaFanart(media, getFanartWidth(),
				getFanartHeight());
		if (result == null) {
			result = getWaitingImg();
			viewType = FanartViewStrategy.WAIT_VIEW_TYPE;
		}

		holder.setFanart(result);
		holder.getRootView().setLayoutParams(getFanartViewLayoutParams());
		holder.setResource(media);
		fanartStrategy.onReturnView(holder, viewType);
		return holder.getRootView();
	}
	
	private Bitmap waitingImg = null;

	protected Bitmap getWaitingImg() {
		if (waitingImg == null) {

			Bitmap fanartImg = BitmapFactory.decodeResource(
					context.getResources(), getWatingImagRes());
			waitingImg = getFanartImgStrategy().handleFanartImg(
					context,
					Bitmap.createScaledBitmap(fanartImg,
							(int) getFanartWidth(), (int) getFanartHeight(),
							true));
			if (fanartImg != null && !fanartImg.isRecycled()) {
				fanartImg.recycle();
			}
		}
		return waitingImg;
	}


	public abstract int dataAddDirection();
	
	protected abstract float getFanartWidth();

	protected abstract float getFanartHeight();
	
	protected abstract LayoutParams getFanartViewLayoutParams();
	
}
